﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Namespace Player

    Module mPlayerChar

        Dim objAnimLock As New Object

        Public Structure HitIndicator
            Dim val As Integer
            Dim xoff As Integer
            Dim yoff As Integer
            Dim movtick As Long
        End Structure

        Public PlayerID As Integer
        Public PlayerName As String
        Public PlayerLevel As Integer
        Public PlayerJob As EvoLib.Protocol.CharacterJobs
        Public PlayerSex As EvoLib.Protocol.Gender
        Public ShapeInfo As EvoLib.Protocol.ShapeInfo
        Public VitalPoints As EvoLib.Protocol.Vitals
        Public IsDead As Boolean
        Public Stats As EvoLib.Protocol.StatsSet
        Public PlayerWeight As PlayerWeights

        Public CurrentXP As Int64 = 0
        Public NextLevelXP As Int64 = Int64.MaxValue

        Public WithEvents mPlayerHumAnim As GUI.MirAnimation
        Private WithEvents mPlayerHeadAnim As GUI.MirAnimation
        Private WithEvents mPlayerWeaponAnim As GUI.MirAnimation

        Private WithEvents mHorseAnim As GUI.MirAnimation
        Private WithEvents mHorseShadowAnim As GUI.MirAnimation

        Private mPlayerChatText As String
        Private mPlayerChatTimeout As Long

        Public Structure PlayerAction
            Dim humanim As EvoLib.Protocol.HUMAnimation
            Dim dir As EvoLib.Protocol.Direction
            Dim magiccode As EvoLib.Protocol.Magics
        End Structure

        Public ActionQueue As List(Of PlayerAction)
        Private LastAction As PlayerAction

        Private isRunning As Boolean
        Private hasWalkedBeforeRun As Boolean
        Private hasRun As Boolean

        Private isWalking As Boolean

        Private isStanding As Boolean

        Private isAttacking As Boolean
        Private attackingCooldown As Long

        Private isGettingHit As Boolean

        Private isCasting1 As Boolean
        Private castingCooldown As Long

        Private isAnimSet As Boolean

        Public AvoidingCollision As Boolean

        Public currentanim As EvoLib.Protocol.HUMAnimation
        Public currentstaticdirection As EvoLib.Protocol.Direction
        Private currentmovementdirection As EvoLib.Protocol.Direction
        Private currentpointerdirection As EvoLib.Protocol.Direction
        Public actualdirection As EvoLib.Protocol.Direction

        Private currentmovementtick As Long
        Private currentmovementtimer As Long
        Private currentmovementdistx As Single
        Private currentmovementdisty As Single

        Private startframe As Integer

        Private hasmoved As Boolean
        Private isstopping As Boolean

        Private lastxcoord As Integer
        Private lastycoord As Integer

        Public lstHits As New List(Of Player.HitIndicator)

        Public isMounting As Boolean
        Public isMounted As Boolean
        Public mHorseShape As Integer

        Public GuildName As String = "[Guild Name]"
        Public GuildTitle As String = "[Guild Title]"

        Public SpouseName As String = "[Spouse Name]"

        Public Gold As Long
        Public GameGold As Long
        Public FamePoints As Long

        Public Enum TargetType
            Mob = 0
        End Enum

        Public mTargetType As TargetType
        Public mTargetID As Integer
        Public mTargeting As Boolean
        Private mAttackTargetMelee As Boolean

        Public Sub Init()
            mPlayerHumAnim = New GUI.MirAnimation(GameWindow.GameWin, "mPlayerHumAnim", GameWindow.DrawAreaStartOffsetX + _
                                                (GameWindow.DrawCentreTileX * Constants.TileWidth), GameWindow.DrawAreaStartOffsetY + _
                                                (GameWindow.DrawCentreTileY * Constants.TileHeight), False)
            mPlayerHumAnim.Visible = False
            mPlayerHeadAnim = New GUI.MirAnimation(GameWindow.GameWin, "mPlayerHeadAnim", GameWindow.DrawAreaStartOffsetX + _
                                                (GameWindow.DrawCentreTileX * Constants.TileWidth), GameWindow.DrawAreaStartOffsetY + _
                                                (GameWindow.DrawCentreTileY * Constants.TileHeight), False)
            mPlayerHeadAnim.Visible = False
            mPlayerWeaponAnim = New GUI.MirAnimation(GameWindow.GameWin, "mPlayerWeaponAnim", GameWindow.DrawAreaStartOffsetX + _
                                                (GameWindow.DrawCentreTileX * Constants.TileWidth), GameWindow.DrawAreaStartOffsetY + _
                                                (GameWindow.DrawCentreTileY * Constants.TileHeight), False)
            mPlayerWeaponAnim.Visible = False
            mHorseAnim = New GUI.MirAnimation(GameWindow.GameWin, "mHorseAnim", GameWindow.DrawAreaStartOffsetX + _
                                                (GameWindow.DrawCentreTileX * Constants.TileWidth), GameWindow.DrawAreaStartOffsetY + _
                                                (GameWindow.DrawCentreTileY * Constants.TileHeight), False)
            mHorseAnim.Visible = False
            mHorseShadowAnim = New GUI.MirAnimation(GameWindow.GameWin, "mHorseShadowAnim", GameWindow.DrawAreaStartOffsetX + _
                                                (GameWindow.DrawCentreTileX * Constants.TileWidth), GameWindow.DrawAreaStartOffsetY + _
                                                (GameWindow.DrawCentreTileY * Constants.TileHeight), False)
            mHorseShadowAnim.Visible = False

            ActionQueue = New List(Of PlayerAction)


            ChangeStaticDirection(EvoLib.Protocol.Direction.S)
            Stand()
            CalcAnim()
        End Sub

        Private Sub ResetAnimBools()
            isStanding = False
            isWalking = False
            isRunning = False
            isAnimSet = False
            startframe = 0
            hasWalkedBeforeRun = False
            hasRun = False
            hasmoved = False
            isstopping = False
            isCasting1 = False
            isAttacking = False
            isGettingHit = False
        End Sub

        Public Sub Run()

            SyncLock objAnimLock

                If Not IsDead Then

                    Dim tmp As PlayerAction

                    If ActionQueue.Count > 0 Then

                        Dim i As Integer
                        Dim seenwalk As Boolean

                        If Not hasWalkedBeforeRun Then
                            Do Until ActionQueue.Count = i

                                tmp = ActionQueue(i)

                                If tmp.humanim = EvoLib.Protocol.HUMAnimation.Walking Or tmp.humanim = EvoLib.Protocol.HUMAnimation.Running Or tmp.humanim = EvoLib.Protocol.HUMAnimation.Standing Then
                                    If tmp.humanim = Protocol.HUMAnimation.Walking And Not seenwalk Then
                                        seenwalk = True
                                    Else
                                        ActionQueue.RemoveAt(i)
                                    End If
                                Else
                                    i += 1
                                End If

                            Loop
                        Else
                            ClearMovements
                        End If


                    End If

                    ClearAttacks()

                    tmp = New PlayerAction

                    tmp.dir = currentstaticdirection
                    tmp.humanim = EvoLib.Protocol.HUMAnimation.Running

                    ActionQueue.Add(tmp)

                    hasWalkedBeforeRun = False

                End If

            End SyncLock

        End Sub

        Public Sub Walk()

            SyncLock objAnimLock

                If Not IsDead Then

                    ClearMovements()
                    ClearAttacks()

                    Dim tmp As PlayerAction
                    tmp.dir = currentstaticdirection
                    tmp.humanim = EvoLib.Protocol.HUMAnimation.Walking

                    ActionQueue.Add(tmp)

                End If

            End SyncLock

        End Sub


        ''' <summary>
        ''' Casts a magic with a throwing action
        ''' </summary>
        ''' <param name="magiccode">Integer representing the magic to be cast</param>
        ''' <remarks></remarks>
        Public Sub Cast1(ByVal magiccode As EvoLib.Protocol.Magics)

            SyncLock objAnimLock

                'check we're not dead
                If Not IsDead Then

                    'check the cooldown
                    If castingCooldown < DateTime.Now.Ticks Then

                        'clear any movement in the action queue
                        ClearMovements()
                        'clear any attacks in the action queue
                        ClearAttacks()

                        'add cast action for this magic to the action queue
                        Dim tmp As PlayerAction
                        tmp.dir = currentpointerdirection
                        tmp.humanim = EvoLib.Protocol.HUMAnimation.Cast1
                        tmp.magiccode = magiccode
                        ActionQueue.Add(tmp)

                        'make the player stand after casting
                        Stand()
                        'delete any duplicate throwing cast actions
                        DeleteExcessActions(EvoLib.Protocol.HUMAnimation.Cast1)

                        'set the cool down period
                        castingCooldown = DateTime.Now.Ticks + 15000000 'TODO:Set different cool downs for individual magics

                    End If

                End If

            End SyncLock

        End Sub

        ''' <summary>
        ''' Casts a magic with an upward action
        ''' </summary>
        ''' <param name="magiccode">Integer representing the magic to be cast</param>
        ''' <remarks></remarks>
        Public Sub Cast2(ByVal magiccode As EvoLib.Protocol.Magics)

            SyncLock objAnimLock

                'check we're not dead
                If Not IsDead Then

                    'check the cooldown
                    If castingCooldown < DateTime.Now.Ticks Then

                        'clear any movement in the action queue
                        ClearMovements()
                        'clear any attacks in the action queue
                        ClearAttacks()

                        'add cast action for this magic to the action queue
                        Dim tmp As PlayerAction
                        Select Case magiccode

                            Case Magics.HealingCast
                                tmp.dir = actualdirection

                            Case Else
                                tmp.dir = currentpointerdirection

                        End Select
                        tmp.humanim = EvoLib.Protocol.HUMAnimation.Cast2
                        tmp.magiccode = magiccode
                        ActionQueue.Add(tmp)

                        'make the player stand after casting
                        Stand()
                        'delete any duplicate throwing cast actions
                        DeleteExcessActions(EvoLib.Protocol.HUMAnimation.Cast2)

                        'set the cool down period
                        castingCooldown = DateTime.Now.Ticks + 15000000 'TODO:Set different cool downs for individual magics

                    End If

                End If

            End SyncLock

        End Sub

        Public Sub Attack()

            If isGettingHit Then Exit Sub

            SyncLock objAnimLock

                If Not IsDead Then

                    If attackingCooldown < DateTime.Now.Ticks Then

                        Dim tmp As PlayerAction
                        tmp.dir = currentstaticdirection
                        tmp.humanim = EvoLib.Protocol.HUMAnimation.Attack

                        ActionQueue.Add(tmp)

                        attackingCooldown = DateTime.Now.Ticks + 10000000

                        Stand()

                        DeleteExcessActions(EvoLib.Protocol.HUMAnimation.Attack)

                    End If

                End If

            End SyncLock

        End Sub

        Public Sub Attack(ByVal ttype As TargetType, ByVal id As Integer)

            If isGettingHit Then Exit Sub

            SyncLock objAnimLock

                If Not IsDead Then

                    mTargeting = True
                    mTargetType = ttype
                    mTargetID = id
                    mAttackTargetMelee = True

                End If

            End SyncLock

        End Sub

        Public Sub ReleaseTarget()

            mTargeting = False
            Magic.PreviousMagicTargetIndex = 0

        End Sub


        ' 
        ' 
        '
        'this does not help problem. but helps debuging
        Private HitCooldown As Long = 7000000
        Public DEBUG_HitTick As Long


        Public Sub Hit()

            If DEBUG_HitTick = 0 Then HitCooldown = EvoLib.Protocol.HUMAnimationTick(EvoLib.Protocol.HUMAnimation.GettingHit) * EvoLib.Protocol.HUMAnimationCounts(EvoLib.Protocol.HUMAnimation.GettingHit)

            'cooldown
            If DEBUG_HitTick > DateTime.Now.Ticks Then
                Exit Sub
            Else
                DEBUG_HitTick = DateTime.Now.Ticks + HitCooldown
            End If

            If Not IsDead Then

                SyncLock objAnimLock

                    Try

                        'ActionQueue.Clear()

                        ClearMovements()
                        ClearAttacks()

                        Dim tmp As PlayerAction

                        tmp.dir = actualdirection
                        tmp.humanim = EvoLib.Protocol.HUMAnimation.GettingHit

                        ActionQueue.Insert(0, tmp)

                        DeleteExcessActions(EvoLib.Protocol.HUMAnimation.GettingHit)

                        isGettingHit = True

                    Catch
                        Debug.Print("Error in Hit()")
                    End Try

                    'GameWindow.MovementOffsetX = 0.0F
                    'GameWindow.MovementOffsetY = 0.0F

                End SyncLock

                Stand()

            End If

        End Sub

        Public Sub Die()

            SyncLock objAnimLock

                mTargeting = False
                IsDead = True
                isGettingHit = False

                Dim tmp As PlayerAction
                tmp.dir = actualdirection
                tmp.humanim = EvoLib.Protocol.HUMAnimation.Die

                ActionQueue.Clear()

                ActionQueue.Add(tmp)

            End SyncLock

        End Sub

        Public Sub Stand()

            SyncLock objAnimLock

                If Not IsDead Then

                    isGettingHit = False

                    ClearMovements()

                    Dim tmp As PlayerAction
                    tmp.dir = currentstaticdirection
                    tmp.humanim = EvoLib.Protocol.HUMAnimation.Standing

                    ActionQueue.Add(tmp)

                End If

            End SyncLock

        End Sub

        Private Sub ClearMovements()

            If ActionQueue.Count = 0 Then Exit Sub

            Dim tmp As PlayerAction
            Dim i As Integer
            Do Until ActionQueue.Count = i

                tmp = ActionQueue(i)
                If tmp.humanim = EvoLib.Protocol.HUMAnimation.Walking Or tmp.humanim = EvoLib.Protocol.HUMAnimation.Running Or tmp.humanim = EvoLib.Protocol.HUMAnimation.Standing Then
                    ActionQueue.RemoveAt(i)
                Else
                    i += 1
                End If

            Loop
        End Sub

        Private Sub ClearAttacks()

            If ActionQueue.Count = 0 Then Exit Sub

            Dim tmp As PlayerAction
            Dim i As Integer
            Do Until ActionQueue.Count = i

                tmp = ActionQueue(i)
                If tmp.humanim = EvoLib.Protocol.HUMAnimation.Attack Or tmp.humanim = EvoLib.Protocol.HUMAnimation.Cast1 Then
                    ActionQueue.RemoveAt(i)
                Else
                    i += 1
                End If

            Loop
        End Sub

        Public Sub ChangeMovementDirection(ByRef dir As EvoLib.Protocol.Direction)

            If ActionQueue.Count = 1 And actualdirection <> dir Then

                Dim tmp As PlayerAction
                tmp.dir = dir
                tmp.humanim = currentanim
                'currentmovementdirection = dir
                currentstaticdirection = dir
                'isAnimSet = False
                'startframe = mPlayerHumAnim.CurrentImg - mPlayerHumAnim.mCurrentAnim.mStartImg
                ClearMovements()
                ActionQueue.Add(tmp)

            End If

        End Sub

        Public Sub ChangeStaticDirection(ByRef dir As EvoLib.Protocol.Direction)
            currentstaticdirection = dir
        End Sub

        Public Sub ChangePointerDirection(ByRef dir As EvoLib.Protocol.Direction)
            currentpointerdirection = dir
        End Sub

        Public Sub DoMove()

            Static lastanim As EvoLib.Protocol.HUMAnimation
            Static lastdir As EvoLib.Protocol.Direction

            If lastxcoord = 0 Then
                lastanim = currentanim
                lastxcoord = GameWindow.CoordX
                lastycoord = GameWindow.CoordY
                lastdir = actualdirection
            End If

            If CInt(GameWindow.MovementOffsetX) = 0 And CInt(GameWindow.MovementOffsetY) = 0 And (lastxcoord <> GameWindow.CoordX Or lastycoord <> GameWindow.CoordY) Then

            Else

                If (currentmovementdistx = 0.0F And currentmovementdisty = 0.0F) Then
                Else

                    If currentmovementtimer + currentmovementtick <= DateTime.Now.Ticks Then

                        hasmoved = True

                        currentmovementtimer += currentmovementtick

                        Select Case actualdirection
                            Case EvoLib.Protocol.Direction.N
                                GameWindow.MovementOffsetY += currentmovementdisty
                            Case EvoLib.Protocol.Direction.NE
                                GameWindow.MovementOffsetY += currentmovementdisty
                                GameWindow.MovementOffsetX -= currentmovementdistx
                            Case EvoLib.Protocol.Direction.E
                                GameWindow.MovementOffsetX -= currentmovementdistx
                            Case EvoLib.Protocol.Direction.SE
                                GameWindow.MovementOffsetY -= currentmovementdisty
                                GameWindow.MovementOffsetX -= currentmovementdistx
                            Case EvoLib.Protocol.Direction.S
                                GameWindow.MovementOffsetY -= currentmovementdisty
                            Case EvoLib.Protocol.Direction.SW
                                GameWindow.MovementOffsetY -= currentmovementdisty
                                GameWindow.MovementOffsetX += currentmovementdistx
                            Case EvoLib.Protocol.Direction.W
                                GameWindow.MovementOffsetX += currentmovementdistx
                            Case EvoLib.Protocol.Direction.NW
                                GameWindow.MovementOffsetY += currentmovementdisty
                                GameWindow.MovementOffsetX += currentmovementdistx
                        End Select

                        If CInt(GameWindow.MovementOffsetX) >= Constants.TileWidth \ 2 Then
                            GameWindow.CoordX -= 1
                            GameWindow.MovementOffsetX = -(Constants.TileWidth - CInt(GameWindow.MovementOffsetX))
                            GameWindow.CalculateDrawingArea()
                            GameWindow.mCurrentMap.UnloadXTiles(actualdirection)
                        ElseIf CInt(GameWindow.MovementOffsetX) <= -Constants.TileWidth \ 2 Then
                            GameWindow.CoordX += 1
                            GameWindow.MovementOffsetX = CInt(GameWindow.MovementOffsetX) + Constants.TileWidth
                            GameWindow.CalculateDrawingArea()
                            GameWindow.mCurrentMap.UnloadXTiles(actualdirection)
                        End If
                        If CInt(GameWindow.MovementOffsetY) >= Constants.TileHeight \ 2 Then
                            GameWindow.CoordY -= 1
                            GameWindow.MovementOffsetY = -(Constants.TileHeight - CInt(GameWindow.MovementOffsetY))
                            GameWindow.CalculateDrawingArea()
                            GameWindow.mCurrentMap.UnloadYTiles(actualdirection)
                        ElseIf CInt(GameWindow.MovementOffsetY) <= -Constants.TileHeight \ 2 Then
                            GameWindow.CoordY += 1
                            GameWindow.MovementOffsetY = CInt(GameWindow.MovementOffsetY) + Constants.TileHeight
                            GameWindow.CalculateDrawingArea()
                            GameWindow.mCurrentMap.UnloadYTiles(actualdirection)
                        End If

                    End If
                End If

            End If

            CalcAnim()

            If CInt(GameWindow.MovementOffsetX) = 0 And CInt(GameWindow.MovementOffsetY) = 0 And (lastxcoord <> GameWindow.CoordX Or lastycoord <> GameWindow.CoordY) Then
                AvoidingCollision = False
            End If

            If CInt(GameWindow.MovementOffsetX) = 0 And CInt(GameWindow.MovementOffsetY) = 0 And (lastanim <> currentanim Or lastdir <> actualdirection Or lastxcoord <> GameWindow.CoordX Or lastycoord <> GameWindow.CoordY) Then
                Dim Packet As EvoLib.Protocol.Packet
                Dim plyrinfo As EvoLib.Protocol.PlayerPositionInfo
                Packet.Username = Globals.sUsername
                Packet.PacketType = EvoLib.Protocol.GamePacketType.PlayerUpdate
                Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry
                plyrinfo.INT2_X = GameWindow.CoordX
                plyrinfo.INT2_Y = GameWindow.CoordY
                plyrinfo.BYTE_ANIM = currentanim
                plyrinfo.BYTE_DIRECTION = actualdirection
                ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, plyrinfo, Globals.Crypto))
                lastanim = currentanim
                lastdir = actualdirection

                Dim movdirX As Integer = GameWindow.CoordX - lastxcoord
                Dim movdirY As Integer = GameWindow.CoordY - lastycoord

                lastxcoord = GameWindow.CoordX
                lastycoord = GameWindow.CoordY

                Dim delx As Integer = lastxcoord - (((GameWindow.DrawAreaTilesX - 1) / 2) * movdirX)
                Dim dely As Integer = lastycoord - (((GameWindow.DrawAreaTilesY - 1) / 2) * movdirY)
                Dim r As Integer
                Dim dolarge As Boolean

                r = Math.DivRem(delx, 2, Nothing)
                dolarge = (r = 0)

                For i As Integer = delx To delx + ((GameWindow.DrawAreaTilesX - 1) * movdirX * -1)

                    If dolarge Then

                        r = Math.DivRem(i, 2, Nothing)
                        If r = 0 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(i, dely).TileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(i, dely).TileSet).Image(GameWindow.mCurrentMap.GetLargeTile(i, dely).Index) Is Nothing Then
                                    If GameWindow.mCurrentMap.GetLargeTile(i, dely).TileSet <> 255 Then
                                        Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(i, dely).TileSet).Image(GameWindow.mCurrentMap.GetLargeTile(i, dely).Index).Unload()
                                    End If
                                End If
                            End If
                        End If
                    End If


                    If GameWindow.mCurrentMap.GetTile(i, dely).BottomTileSet <> 255 Then
                        If GameWindow.mCurrentMap.GetTile(i, dely).BottomTileIndex <> 65535 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).BottomTileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).BottomTileSet).Image(GameWindow.mCurrentMap.GetTile(i, dely).BottomTileIndex) Is Nothing Then

                                    Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).BottomTileSet).Image(GameWindow.mCurrentMap.GetTile(i, dely).BottomTileIndex).Unload()
                                End If
                            End If
                        End If
                    End If

                    If GameWindow.mCurrentMap.GetTile(i, dely).TopTileSet <> 255 Then
                        If GameWindow.mCurrentMap.GetTile(i, dely).TopTileIndex <> 65535 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).TopTileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).TopTileSet).Image(GameWindow.mCurrentMap.GetTile(i, dely).TopTileIndex) Is Nothing Then

                                    Libs.GetLib(GameWindow.mCurrentMap.GetTile(i, dely).TopTileSet).Image(GameWindow.mCurrentMap.GetTile(i, dely).TopTileIndex).Unload()
                                End If
                            End If
                        End If
                    End If

                Next

                r = Math.DivRem(dely, 2, Nothing)
                dolarge = (r = 0)

                For i As Integer = dely + movdirY To dely + ((GameWindow.DrawAreaTilesY - 1) * movdirY * -1)

                    If dolarge Then

                        r = Math.DivRem(i, 2, Nothing)
                        If r = 0 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(delx, i).TileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(delx, i).TileSet).Image(GameWindow.mCurrentMap.GetLargeTile(delx, i).Index) Is Nothing Then
                                    If GameWindow.mCurrentMap.GetLargeTile(delx, i).TileSet <> 255 Then
                                        Libs.GetLib(GameWindow.mCurrentMap.GetLargeTile(delx, i).TileSet).Image(GameWindow.mCurrentMap.GetLargeTile(delx, i).Index).Unload()
                                    End If
                                End If
                            End If
                        End If
                    End If


                    If GameWindow.mCurrentMap.GetTile(delx, i).BottomTileSet <> 255 Then
                        If GameWindow.mCurrentMap.GetTile(delx, i).BottomTileIndex <> 65535 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).BottomTileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).BottomTileSet).Image(GameWindow.mCurrentMap.GetTile(delx, i).BottomTileIndex) Is Nothing Then

                                    Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).BottomTileSet).Image(GameWindow.mCurrentMap.GetTile(delx, i).BottomTileIndex).Unload()
                                End If
                            End If
                        End If
                    End If

                    If GameWindow.mCurrentMap.GetTile(delx, i).TopTileSet <> 255 Then
                        If GameWindow.mCurrentMap.GetTile(delx, i).TopTileIndex <> 65535 Then
                            If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).TopTileSet) Is Nothing Then
                                If Not Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).TopTileSet).Image(GameWindow.mCurrentMap.GetTile(delx, i).TopTileIndex) Is Nothing Then

                                    Libs.GetLib(GameWindow.mCurrentMap.GetTile(delx, i).TopTileSet).Image(GameWindow.mCurrentMap.GetTile(delx, i).TopTileIndex).Unload()
                                End If
                            End If
                        End If
                    End If

                Next

            End If

        End Sub

        Private Function DoNextSteps() As Boolean

            Dim pt As Point = CalcNextStep(GameWindow.CoordX, GameWindow.CoordY, actualdirection)
            If Not GameWindow.mCurrentMap.IsPassable(pt.X, pt.Y) Then

                Dim tmpDir As EvoLib.Protocol.Direction

                Select Case actualdirection

                    Case EvoLib.Protocol.Direction.N
                        If GameWindow.MouseAngle > 337.5 Then
                            tmpDir = EvoLib.Protocol.Direction.NW
                        Else
                            tmpDir = EvoLib.Protocol.Direction.NE
                        End If

                    Case EvoLib.Protocol.Direction.NE
                        If GameWindow.MouseAngle < 45 Then
                            tmpDir = EvoLib.Protocol.Direction.N
                        Else
                            tmpDir = EvoLib.Protocol.Direction.E
                        End If

                    Case EvoLib.Protocol.Direction.E
                        If GameWindow.MouseAngle < 90 Then
                            tmpDir = EvoLib.Protocol.Direction.NE
                        Else
                            tmpDir = EvoLib.Protocol.Direction.SE
                        End If

                    Case EvoLib.Protocol.Direction.SE
                        If GameWindow.MouseAngle < 135 Then
                            tmpDir = EvoLib.Protocol.Direction.E
                        Else
                            tmpDir = EvoLib.Protocol.Direction.S
                        End If

                    Case EvoLib.Protocol.Direction.S
                        If GameWindow.MouseAngle < 180 Then
                            tmpDir = EvoLib.Protocol.Direction.SE
                        Else
                            tmpDir = EvoLib.Protocol.Direction.SW
                        End If

                    Case EvoLib.Protocol.Direction.SW
                        If GameWindow.MouseAngle < 225 Then
                            tmpDir = EvoLib.Protocol.Direction.S
                        Else
                            tmpDir = EvoLib.Protocol.Direction.W
                        End If

                    Case EvoLib.Protocol.Direction.W
                        If GameWindow.MouseAngle < 270 Then
                            tmpDir = EvoLib.Protocol.Direction.SW
                        Else
                            tmpDir = EvoLib.Protocol.Direction.NW
                        End If

                    Case EvoLib.Protocol.Direction.NW
                        If GameWindow.MouseAngle < 315 Then
                            tmpDir = EvoLib.Protocol.Direction.W
                        Else
                            tmpDir = EvoLib.Protocol.Direction.N
                        End If

                End Select

                pt = CalcNextStep(GameWindow.CoordX, GameWindow.CoordY, tmpDir)

                If Not GameWindow.mCurrentMap.IsPassable(pt.X, pt.Y) Then

                    Return False

                Else

                    If isRunning Then

                        pt = CalcNextStep(pt.X, pt.Y, tmpDir)
                        If Not GameWindow.mCurrentMap.IsPassable(pt.X, pt.Y) Then
                            Walk()
                        End If

                        actualdirection = tmpDir
                        AvoidingCollision = True
                        Return True

                    Else

                        actualdirection = tmpDir
                        AvoidingCollision = True
                        Return True

                    End If

                End If

            End If

            Return True


        End Function

        Public Sub CalcAnim(Optional ByVal overidecentrelock As Boolean = False, Optional ByVal overrideanimlock As Boolean = False)

            Dim tmphumanim As GUI.Animation = Nothing
            Dim tmpheadanim As GUI.Animation = Nothing
            Dim tmpweapanim As GUI.Animation = Nothing
            Dim bLoop As Boolean = True

            Static AnimTimer As Long

            If DateTime.Now.Ticks < AnimTimer Then Exit Sub

            If mTargeting And mAttackTargetMelee And (CInt(GameWindow.MovementOffsetX) = 0 And CInt(GameWindow.MovementOffsetY) = 0) Then
                If Not TargetIsDead() Then

                    Dim targetpos As Point = GetTargetPos()

                    If targetpos.X > 0 Then

                        Dim x As Integer, y As Integer

                        x = GameWindow.CoordX - targetpos.X
                        y = GameWindow.CoordY - targetpos.Y

                        Dim dir As EvoLib.Protocol.Direction

                        If x = 0 And y > 0 Then
                            dir = EvoLib.Protocol.Direction.N
                        ElseIf x = 0 And y < 0 Then
                            dir = EvoLib.Protocol.Direction.S
                        ElseIf y = 0 And x > 0 Then
                            dir = EvoLib.Protocol.Direction.W
                        ElseIf y = 0 And x < 0 Then
                            dir = EvoLib.Protocol.Direction.E
                        ElseIf x > 0 And y > 0 Then
                            dir = EvoLib.Protocol.Direction.NW
                        ElseIf x < 0 And y < 0 Then
                            dir = EvoLib.Protocol.Direction.SE
                        ElseIf x < 0 And y > 0 Then
                            dir = EvoLib.Protocol.Direction.NE
                        Else
                            dir = EvoLib.Protocol.Direction.SW
                        End If

                        If x < 2 And x > -2 And y < 2 And y > -2 And Not (x = 0 And y = 0) Then
                            'attack

                            ChangeStaticDirection(dir)
                            Attack()

                        Else
                            'move

                            Dim pt As Point
                            Dim tries As Integer
                            Dim tmpdir As Integer = dir

                            Do

                                pt = CalcNextStep(GameWindow.CoordX, GameWindow.CoordY, tmpdir)

                                If GameWindow.mCurrentMap.IsPassable(pt.X, pt.Y) Then Exit Do


                                tmpdir += 1
                                If tmpdir = 8 Then tmpdir = 0
                                tries += 1

                                If tries = 8 Then

                                    mTargeting = False
                                    ChangeStaticDirection(dir)
                                    Stand()
                                    Exit Sub

                                End If

                            Loop

                            ChangePointerDirection(tmpdir)
                            Walk()

                        End If

                    Else
                        mTargeting = False
                        GameWindow.MovementOffsetX = 0
                        GameWindow.MovementOffsetY = 0
                        Stand()


                    End If

                Else

                    mTargeting = False
                    GameWindow.MovementOffsetX = 0
                    GameWindow.MovementOffsetY = 0
                    Stand()

                End If

            End If

            Dim action As PlayerAction
            Dim prevanim As EvoLib.Protocol.HUMAnimation = LastAction.humanim

            If ActionQueue.Count = 0 And Not overrideanimlock Then
                Exit Sub
            ElseIf overrideanimlock Then
                Action = LastAction
            Else
                Action = ActionQueue.Item(0)
                If Action.humanim = EvoLib.Protocol.mProtocol.HUMAnimation.GettingHit And LastAction.humanim = EvoLib.Protocol.mProtocol.HUMAnimation.GettingHit And startframe = EvoLib.Protocol.HUMAnimationCounts(EvoLib.Protocol.HUMAnimation.GettingHit) - 1 Then
                    ActionQueue.Clear()
                    Stand()
                    CalcAnim()
                    Exit Sub
                Else
                    LastAction = Action
                End If
            End If

            Application.DoEvents()

            If (Action.humanim <> currentanim Or currentpointerdirection <> actualdirection Or lastxcoord <> GameWindow.CoordX Or lastycoord <> GameWindow.CoordY Or overrideanimlock) And Not AvoidingCollision Then

                If ((CInt(GameWindow.MovementOffsetX) = 0 And CInt(GameWindow.MovementOffsetY) = 0)) Or overidecentrelock Or currentanim = EvoLib.Protocol.HUMAnimation.GettingHit Then

                    If overrideanimlock Or isWalking Or isRunning Then startframe = mPlayerHumAnim.CurrentImg - mPlayerHumAnim.mCurrentAnim.mStartImg

                    If Action.humanim = EvoLib.Protocol.HUMAnimation.Walking Then

                        actualdirection = currentpointerdirection
                        'Application.DoEvents()

                        If Not DoNextSteps() Then

                            Stand()

                        Else

                            tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Walking)
                            isRunning = False

                        End If


                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Running Then

                        actualdirection = currentpointerdirection
                        'Application.DoEvents()

                        If Not DoNextSteps() Then

                            Stand()

                        Else

                            If Not hasWalkedBeforeRun And Not isRunning Then

                                tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Walking)
                                bLoop = False
                                isRunning = True

                            Else

                                If hasWalkedBeforeRun Then

                                    tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Running)

                                    If Not hasRun Then
                                        hasRun = True
                                    End If

                                    bLoop = True

                                End If

                            End If

                        End If

                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Attack Then

                        If currentanim = EvoLib.Protocol.HUMAnimation.Attack Then
                            DeleteExcessActions(EvoLib.Protocol.mProtocol.HUMAnimation.Attack, True)
                            If ActionQueue.Count = 0 Then Stand()
                        Else
                            actualdirection = Action.dir
                            tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Attack)
                            bLoop = False
                            AnimTimer = DateTime.Now.Ticks + TimeForHUMAnim(EvoLib.Protocol.HUMAnimation.Attack)
                        End If

                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Cast1 Then

                        If currentanim = EvoLib.Protocol.mProtocol.HUMAnimation.Cast1 Then
                            DeleteExcessActions(EvoLib.Protocol.mProtocol.HUMAnimation.Cast1, True)
                            If ActionQueue.Count = 0 Then Stand()
                        Else
                            actualdirection = Action.dir
                            tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Cast1)
                            bLoop = False
                            AnimTimer = DateTime.Now.Ticks + TimeForHUMAnim(EvoLib.Protocol.HUMAnimation.Cast1)

                            Magic.AddCastMagic(Action.magiccode, Action.dir)
                        End If

                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Cast2 Then
2:
                        If currentanim = EvoLib.Protocol.mProtocol.HUMAnimation.Cast2 Then
                            DeleteExcessActions(EvoLib.Protocol.mProtocol.HUMAnimation.Cast2, True)
                            If ActionQueue.Count = 0 Then Stand()
                        Else
                            actualdirection = Action.dir
                            tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Cast2)
                            bLoop = False
                            AnimTimer = DateTime.Now.Ticks + TimeForHUMAnim(EvoLib.Protocol.HUMAnimation.Cast2)

                            Magic.AddCastMagic(Action.magiccode, Action.dir)
                        End If

                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Die Then

                        actualdirection = Action.dir
                        tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Die)
                        bLoop = False

                    ElseIf Action.humanim = EvoLib.Protocol.HUMAnimation.Standing Then

                        actualdirection = Action.dir
                        tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.Standing)
                        hasWalkedBeforeRun = False
                        bLoop = True
                        isRunning = False

                    End If

                End If

                If Action.humanim = EvoLib.Protocol.HUMAnimation.GettingHit Then

                    'If startframe > 0 Then

                    'DeleteExcessActions(EvoLib.Protocol.mProtocol.HUMAnimation.GettingHit, True)
                    'Stand()
                    GameWindow.MovementOffsetX = 0.0F
                    GameWindow.MovementOffsetY = 0.0F
                    'isGettingHit = False
                    'CalcAnim()

                    If currentanim = EvoLib.Protocol.mProtocol.HUMAnimation.GettingHit Then
                        DeleteExcessActions(EvoLib.Protocol.mProtocol.HUMAnimation.GettingHit, True)
                        Stand()
                    End If
                    'Else

                    actualdirection = Action.dir
                    tmphumanim = GetPlayerHumAnim(EvoLib.Protocol.HUMAnimation.GettingHit)
                    bLoop = False
                    hasWalkedBeforeRun = False
                    isRunning = False

                    AnimTimer = DateTime.Now.Ticks + TimeForHUMAnim(EvoLib.Protocol.HUMAnimation.GettingHit)

                    'End If

                End If

                Application.DoEvents()

                If Not tmphumanim Is Nothing Then

                    If currentanim = EvoLib.Protocol.HUMAnimation.Running Or currentanim = EvoLib.Protocol.HUMAnimation.Walking Then

                        If prevanim = EvoLib.Protocol.HUMAnimation.Standing Then

                            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.StartWalking), 100)

                        Else

                            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Walking), 100)

                        End If

                    ElseIf (prevanim = EvoLib.Protocol.HUMAnimation.Standing Or prevanim = EvoLib.Protocol.HUMAnimation.Running) And currentanim = EvoLib.Protocol.HUMAnimation.Walking Then

                        Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.StopWalking), 100)

                    End If

                    If currentanim = EvoLib.Protocol.mProtocol.HUMAnimation.Die And Action.humanim = HUMAnimation.Die Then
                        mPlayerHumAnim.StartAnim(tmphumanim, bLoop, ShapeInfo.INT_ARMORCOL, HUMAnimationCounts(HUMAnimation.Die))

                        tmpheadanim = GetPlayerHeadAnim()
                        mPlayerHeadAnim.StartAnim(tmpheadanim, bLoop, ShapeInfo.INT_HAIRCOL, HUMAnimationCounts(HUMAnimation.Die))

                        tmpweapanim = GetPlayerWeaponAnim()
                        mPlayerWeaponAnim.StartAnim(tmpweapanim, bLoop, , HUMAnimationCounts(HUMAnimation.Die))
                        mPlayerWeaponAnim.Visible = True

                    Else

                        mPlayerHumAnim.StartAnim(tmphumanim, bLoop, ShapeInfo.INT_ARMORCOL, startframe)

                        tmpheadanim = GetPlayerHeadAnim()
                        mPlayerHeadAnim.StartAnim(tmpheadanim, bLoop, ShapeInfo.INT_HAIRCOL, startframe)

                        If Not isMounted Then
                            tmpweapanim = GetPlayerWeaponAnim()
                            mPlayerWeaponAnim.StartAnim(tmpweapanim, bLoop, , startframe)
                            mPlayerWeaponAnim.Visible = True
                        Else
                            mPlayerWeaponAnim.Visible = False
                        End If


                        startframe = 0

                    End If

                End If

            End If

        End Sub

        Private Function GetHorseAnim(ByRef anim As EvoLib.Protocol.HUMAnimation) As GUI.Animation

            Dim A As Integer

            Select Case anim

                Case EvoLib.Protocol.HUMAnimation.MountedStand
                    A = 0

                Case EvoLib.Protocol.HUMAnimation.MountedWalk
                    A = 1

                Case EvoLib.Protocol.HUMAnimation.MountedRun
                    A = 2

            End Select


            Return New GUI.Animation(EvoLib.Protocol.ImgLibs.Horse, (mHorseShape * 400) + 1 + (A * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(anim), EvoLib.Protocol.HUMAnimationTick(anim))

        End Function

        Private Function GetPlayerHumAnim(ByRef anim As EvoLib.Protocol.HUMAnimation) As GUI.Animation

            currentanim = anim

            Dim tmphorseanim As GUI.Animation = Nothing
            Dim tmphorseshadowanim As GUI.Animation = Nothing

            If isMounting Or isMounted Then
                mHorseAnim.Visible = True
                mHorseShadowAnim.Visible = True
                isMounting = False
                isMounted = True
            End If

            If isMounted Then
                If anim = EvoLib.Protocol.HUMAnimation.Standing Then anim = EvoLib.Protocol.HUMAnimation.MountedStand
                If anim = EvoLib.Protocol.HUMAnimation.Running Then anim = EvoLib.Protocol.HUMAnimation.MountedRun
                If anim = EvoLib.Protocol.HUMAnimation.Walking Then anim = EvoLib.Protocol.HUMAnimation.MountedWalk
                tmphorseanim = GetHorseAnim(anim)

                Dim c As Integer
                If Not mHorseAnim.mCurrentAnim Is Nothing Then
                    c = (mHorseAnim.CurrentImg - mHorseAnim.mCurrentAnim.mStartImg)
                End If

                mHorseAnim.StartAnim(tmphorseanim, True, , c)
                mHorseAnim.Visible = True
            Else
                mHorseAnim.Visible = False
            End If

            currentmovementtick = EvoLib.Protocol.HUMAnimationMoveTick(anim)

            If IsOverencumbered(Stats, PlayerWeight) Then

                currentmovementtick = currentmovementtick * Constants.OverencumberedFactor

            End If

            currentmovementtimer = DateTime.Now.Ticks
            currentmovementdistx = EvoLib.Protocol.HUMAnimationDistanceX(anim)
            currentmovementdisty = EvoLib.Protocol.HUMAnimationDistanceY(anim)

            'anim = Globals.DEBUG_X(0)

            Dim tmp As Libs.LibShapePair = Libs.GetArmourLib(PlayerSex, ShapeInfo.INT_ARMORSHAPE)
            If Player.PlayerWeight.INT4_BAGWEIGHT > Player.Stats.INT2_MAX_WEIGHT Then
                Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (anim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(anim), EvoLib.Protocol.HUMAnimationTick(anim) * Constants.OverencumberedFactor)
            Else
                Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (anim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(anim), EvoLib.Protocol.HUMAnimationTick(anim))
            End If
        End Function

        Private Function GetPlayerHeadAnim() As GUI.Animation

            If ShapeInfo.INT_HEADSHAPE = 22 And Not ShapeInfo.BOOL_HEADISHELM Then
                Return Nothing
            Else

                Dim tmp As Libs.LibShapePair
                If ShapeInfo.BOOL_HEADISHELM And ShapeInfo.INT_HEADSHAPE >= 0 Then
                    tmp = Libs.GetHeadLib(PlayerSex, ShapeInfo.INT_HEADSHAPE, ShapeInfo.BOOL_HEADISHELM)
                Else
                    tmp = Libs.GetHeadLib(PlayerSex, ShapeInfo.INT_HAIRSHAPE, False)
                End If

                If isMounted Then
                    Dim a As EvoLib.Protocol.HUMAnimation
                    Select Case currentanim
                        Case EvoLib.Protocol.HUMAnimation.Standing
                            a = EvoLib.Protocol.HUMAnimation.MountedStand
                        Case EvoLib.Protocol.HUMAnimation.Walking
                            a = EvoLib.Protocol.HUMAnimation.MountedWalk
                        Case EvoLib.Protocol.HUMAnimation.Running
                            a = EvoLib.Protocol.HUMAnimation.MountedRun

                    End Select

                    If Player.PlayerWeight.INT4_BAGWEIGHT > Player.Stats.INT2_MAX_WEIGHT Then
                        Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (a * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(a), EvoLib.Protocol.HUMAnimationTick(a) * Constants.OverencumberedFactor)
                    Else
                        Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (a * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(a), EvoLib.Protocol.HUMAnimationTick(a))
                    End If
                Else
                    If Player.PlayerWeight.INT4_BAGWEIGHT > Player.Stats.INT2_MAX_WEIGHT Then
                        Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (currentanim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(currentanim), EvoLib.Protocol.HUMAnimationTick(currentanim) * Constants.OverencumberedFactor)
                    Else
                        Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (currentanim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(currentanim), EvoLib.Protocol.HUMAnimationTick(currentanim))
                    End If
                End If
            End If

        End Function

        Private Function GetPlayerWeaponAnim() As GUI.Animation

            If ShapeInfo.INT_WEAPONSHAPE = -1 Then Return Nothing

            Dim tmp As Libs.LibShapePair = Libs.GetWeaponLib(PlayerSex, ShapeInfo.INT_WEAPONSHAPE)
            If Player.PlayerWeight.INT4_BAGWEIGHT > Player.Stats.INT2_MAX_WEIGHT Then
                Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (currentanim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(currentanim), EvoLib.Protocol.HUMAnimationTick(currentanim) * Constants.OverencumberedFactor)
            Else
                Return New GUI.Animation(tmp.library, (tmp.shape * 3000) + 1 + (currentanim * 80) + (actualdirection * 10), EvoLib.Protocol.HUMAnimationCounts(currentanim), EvoLib.Protocol.HUMAnimationTick(currentanim))
            End If

        End Function


        Public Function CalcNextStep(ByVal cx As Integer, ByVal cy As Integer, ByVal dir As EvoLib.Protocol.Direction) As Point

            Dim ret As New Point(cx, cy)

            Select Case dir
                Case EvoLib.Protocol.Direction.N
                    ret.Y -= 1
                Case EvoLib.Protocol.Direction.NE
                    ret.Y -= 1
                    ret.X += 1
                Case EvoLib.Protocol.Direction.E
                    ret.X += 1
                Case EvoLib.Protocol.Direction.SE
                    ret.Y += 1
                    ret.X += 1
                Case EvoLib.Protocol.Direction.S
                    ret.Y += 1
                Case EvoLib.Protocol.Direction.SW
                    ret.Y += 1
                    ret.X -= 1
                Case EvoLib.Protocol.Direction.W
                    ret.X -= 1
                Case EvoLib.Protocol.Direction.NW
                    ret.Y -= 1
                    ret.X -= 1
            End Select

            Return ret

        End Function

        Private Sub mPlayerHumAnim_AnimStopped() Handles mPlayerHumAnim.AnimStopped

            'Debug.Print("mPlayerHumAnim_AnimStopped()]")

            isGettingHit = False

            If currentanim = EvoLib.Protocol.HUMAnimation.Walking Then

                hasWalkedBeforeRun = True

                CalcAnim()

                'ElseIf isCasting1 Then
                'ResetAnimBools()
                'Stand()
                'CalcAnim()
                'ElseIf isAttacking Then
                'ResetAnimBools()
                'Stand()
                'CalcAnim()

            ElseIf currentanim = EvoLib.Protocol.HUMAnimation.GettingHit Then

                If ActionQueue.Count > 0 Then ActionQueue.RemoveAt(0)
                Stand()
                CalcAnim(True)

                'If ActionQueue.Count > 1 Then ActionQueue.RemoveAt(0)
            ElseIf currentanim = EvoLib.Protocol.HUMAnimation.Die Then

                ActionQueue.Clear()

            Else

                ActionQueue.RemoveAt(0)
                Stand()
                CalcAnim()

            End If

        End Sub

        Public Sub DrawPlayer(ByRef mode As Globals.DrawingMode)

            If mode = Globals.DrawingMode.Text Then

                Dim dx As Integer = ((GameWindow.DrawAreaStartOffsetX + (GameWindow.DrawCentreTileX * Constants.TileWidth)))
                Dim dy As Integer = ((GameWindow.DrawAreaStartOffsetY + (GameWindow.DrawCentreTileY * Constants.TileHeight)))

                If mPlayerChatTimeout > 0 Then

                    If mPlayerChatTimeout > DateTime.Now.Ticks Then

                        'compute text position and size
                        Dim tW As Integer = DirectX.ProFont9.GetNormalTextWidth(mPlayerChatText)
                        If tW >= Constants.ChatMessageWidth Then tW = Constants.ChatMessageWidth
                        Dim tH As Integer = DirectX.ProFont9.GetNormalTextHeight(mPlayerChatText, tW)

                        'destination rectangle
                        Dim Dest As Rectangle = New Rectangle(dx + (Constants.TileWidth \ 2) - (tW \ 2), dy - CInt(CSng(Constants.TileHeight) * 2.4F), tW, tH + Constants.TileHeight)
                        'DrawTextFormat flags
                        Dim fmt As DrawTextFormat = DrawTextFormat.Center Or _
                                                    DrawTextFormat.ExpandTabs Or _
                                                    DrawTextFormat.WordBreak Or _
                                                    DrawTextFormat.NoClip
                        'draw chart text
                        DirectX.ProFont9.DrawBorderText(mPlayerChatText, Dest, fmt, Color.White, Color.Black, Constants.GameTextZ)

                    Else
                        'set chat timeout var to 0
                        mPlayerChatTimeout = 0
                    End If

                End If

                'calculate 'Name Text' width
                Dim tW2 As Integer = DirectX.ProFont9.GetNormalTextWidth(PlayerName)
                'Draw 'Name Text'
                DirectX.ProFont9.DrawBorderTextEx(PlayerName, dx + (Constants.TileWidth \ 2) - (tW2 \ 2), dy - Constants.TileHeight, Color.White, Color.Black, Constants.GameTextZ)

            ElseIf mode = Globals.DrawingMode.Normal Or mode = Globals.DrawingMode.Tinted Then

                mPlayerHumAnim.Visible = True
                mPlayerHumAnim.Draw(mode, 0, 0, 0.5F)
                mPlayerHumAnim.Visible = False

                mPlayerHeadAnim.Visible = True
                mPlayerHeadAnim.Draw(mode, 0, 0, 0.4F)
                mPlayerHeadAnim.Visible = False

                If mode = Globals.DrawingMode.Normal Then

                    Dim weapz As Single

                    Select Case actualdirection
                        Case EvoLib.Protocol.Direction.N
                            weapz = 0.6F
                        Case EvoLib.Protocol.Direction.NE
                            weapz = 0.4F
                        Case EvoLib.Protocol.Direction.E
                            weapz = 0.4F
                        Case EvoLib.Protocol.Direction.SE
                            weapz = 0.4F
                        Case EvoLib.Protocol.Direction.S
                            weapz = 0.4F
                        Case EvoLib.Protocol.Direction.SW
                            weapz = 0.6F
                        Case EvoLib.Protocol.Direction.W
                            weapz = 0.6F
                        Case EvoLib.Protocol.Direction.NW
                            weapz = 0.6F
                    End Select

                    If Not isMounted Then
                        mPlayerWeaponAnim.Visible = True
                        mPlayerWeaponAnim.Draw(mode, 0, 0, weapz)
                        mPlayerWeaponAnim.Visible = False
                    End If

                    
                        'Draw Hit Indicators
                    DrawHitIndicators(lstHits, GameWindow.DrawAreaStartOffsetX + (GameWindow.DrawCentreTileX * Constants.TileWidth), _
                                        GameWindow.DrawAreaStartOffsetY + (GameWindow.DrawCentreTileY * Constants.TileHeight))

                End If

            End If

        End Sub

        Private HeaderIdx As Integer
        Private HeaderTick As Long = DateTime.Now.Ticks

        Public Sub DrawPlayerTypeHeader(T As PlayerType, x As Integer, y As Integer)

            If HeaderTick < DateTime.Now.Ticks Then
                HeaderTick = DateTime.Now.Ticks + 5000000
                HeaderIdx += 1
                If HeaderIdx > 6 Then HeaderIdx = 0
            End If

            Select Case T

                Case PlayerType.DEV
                    With Libs.GameInter.Image(3411 + HeaderIdx)

                        .Draw(x + .Header.OffsetX + 10, y + .Header.OffsetY - 22, Constants.ActorTypeZ)

                    End With

            End Select

        End Sub

        Public Sub DrawMain(ByRef mode As Globals.DrawingMode, ByRef z As Single)

            If mode = Globals.DrawingMode.Normal Then
                DirectX.BlitD3D(DirectX.PlayerTexture, 0, 0, Constants.gGameResX, Constants.gGameResY, z)
            End If

        End Sub

        Public Sub DrawShadow(ByVal mode As Globals.DrawingMode, ByVal z As Single)

            If mode = Globals.DrawingMode.Shadow Then

                DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                Engine.SetDrawModeShadow(Globals.ShadowBlend)
                DirectX.GameSprite.Transform = Microsoft.DirectX.Matrix.Transformation(New Microsoft.DirectX.Vector3(Constants.gGameResX / 2, Constants.gGameResY / 2, 0.0F), Microsoft.DirectX.Quaternion.Identity, New Microsoft.DirectX.Vector3(1.0F, 1.0F, 1.0F), New Microsoft.DirectX.Vector3(mPlayerHumAnim.PosX, mPlayerHumAnim.PosY, z), New Microsoft.DirectX.Quaternion(-0.5F, 0.3F, 0.0F, 0.0F), New Microsoft.DirectX.Vector3(10.0F, 10.0F, 0.0F))
                'DirectX.GameSprite.Transform = Matrix.Transformation2D(New Vector2(mPlayerHumAnim.PosX, mPlayerHumAnim.PosY), 0.0F, New Vector2(0.0, 0.0), New Vector2(mPlayerHumAnim.PosX, mPlayerHumAnim.PosY), 0.0F, New Vector2(0.0F, 0.0F))
                DirectX.GameSprite.Draw(DirectX.PlayerTexture, New Rectangle(0, 0, Constants.gGameResX, Constants.gGameResY), New Microsoft.DirectX.Vector3(0.0F, 0.0F, 0.0F), New Microsoft.DirectX.Vector3(0.0F, 0.0F, z), Color.White)
                DirectX.GameSprite.End()

                DirectX.GameSprite.Transform = Microsoft.DirectX.Matrix.Identity
            End If

        End Sub

        Public Sub DrawAlpha(ByRef mode As Globals.DrawingMode, ByRef z As Single)

            If mode = Globals.DrawingMode.SemiTransparent Then
                DirectX.BlitD3D(DirectX.PlayerTexture, 0, 0, Constants.gGameResX, Constants.gGameResY, z)
            End If

        End Sub

        Public Sub ChatMessage(ByRef msg As EvoLib.Protocol.CharChatMsg, ByRef tc As Long)

            'Set the chat text string to the message that was sent
            mPlayerChatText = msg.STR_NAME & ": " & msg.msg.STR_MSG
            'Set the timeout for displaying the chat message text
            mPlayerChatTimeout = tc + Constants.ChatMessageDisplayTime

        End Sub

        Private Function GetTargetPos() As Point

            Select Case mTargetType

                Case TargetType.Mob
                    Return Mobs.GetMobPos(mTargetID)

            End Select

        End Function

        Private Function TargetIsDead() As Boolean

            Select Case mTargetType

                Case TargetType.Mob
                    Return Mobs.MobIsDead(mTargetID)

            End Select

        End Function

        Private Sub DeleteExcessActions(ByVal anim As EvoLib.Protocol.HUMAnimation, Optional ByVal deleteall As Boolean = False)

            Dim foundfirstentry As Boolean
            Dim i As Integer

            Do Until i = ActionQueue.Count

                If ActionQueue(i).humanim = anim Then

                    If deleteall Then
                        ActionQueue.RemoveAt(i)
                    Else
                        If foundfirstentry Then
                            ActionQueue.RemoveAt(i)
                        Else
                            foundfirstentry = True
                            i += 1
                        End If
                    End If

                Else

                    i += 1

                End If

            Loop

        End Sub

        Private Sub mPlayerHeadAnim_AnimStopped(ByVal name As String) Handles mPlayerHeadAnim.AnimStopped

        End Sub

        Public Sub ToggleMount()

            If currentanim = EvoLib.Protocol.HUMAnimation.Standing Then
                isMounting = Not isMounting
            End If

        End Sub

        Public Sub UseItem(id As Int64)

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.UseItem
            pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            ClientSock.gSock.SendData(MakePacket(pkt, id, Globals.Crypto))

        End Sub

        Public Function TimeForHUMAnim(anim As HUMAnimation) As Long

            Return EvoLib.Protocol.HUMAnimationCounts(anim) * EvoLib.Protocol.HUMAnimationTick(anim)

        End Function

    End Module


End Namespace